﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallERP.MODEL.FoundationModel;
using System.Windows.Forms;
using SmallERP.COM;

namespace SmallERP.Utilities
{
    public abstract class CommonClass
    {
        public string ServerIP { get; set; }
        public int ServerPort { get; set; }
        #region 公共的
        private static StartUPUtil _upBLL;
        private static bool isDoing = false;
        static void t_Tick(object sender)
        {
            if (!isDoing)
            {
                isDoing = true;
                if (_upBLL == null)
                {
                    _upBLL = new StartUPUtil();
                }
                ServerInfo serverInfo = new ServerInfo();
                string errMsg = string.Empty;
                int result = _upBLL.GetServerInfo(out serverInfo, out errMsg);
                if (result != 0)
                {
                    MessageBox.Show("与服务器失去连接，程序将立即重启，请检查与服务器的连接！");
                    Environment.Exit(-1);
                }
                isDoing = false;
            }
        }

        private bool DealResult()
        {
            if (_upBLL == null)
            {
                _upBLL = new StartUPUtil();
            }
            ServerInfo serverInfo = new ServerInfo();
            string errMsg = string.Empty;
            int result = _upBLL.GetServerInfo(out serverInfo, out errMsg);
            if (result != 0)//有问题
            {
                MessageBox.Show(errMsg);

                if (result == -6)
                {
                    Application.ApplicationExit += new EventHandler(Restart);
                }

                Application.Exit();
                return false;
            }
            ServerIP = serverInfo.ServerIP;
            ServerPort = serverInfo.ServerTCPPort;
            return true;
        }

        public abstract void Restart(object sender, EventArgs e);
        public abstract void SetServerIPAndPort();
        
        #endregion
    }
}
